#include <event2/event.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <iostream>
#include <signal.h>
#include <assert.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <string>
using namespace std;

static const char *ip = "192.168.232.128";
static const int port = 12345;


bufferevent_filter_result filter_in(
    struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit,
    enum bufferevent_flush_mode mode, void *ctx) {
    
    cout << "filter in" << endl;
    char data[1024] = {0};
    int len = evbuffer_remove(src, data, sizeof(data) - 1);

    // 转换成大写字母
    for (int i = 0; i < len; i++) {
        data[i] = toupper(data[i]);
    }
    evbuffer_add(dst, data, len);
    return BEV_OK;
}

bufferevent_filter_result filter_out(
    struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit,
    enum bufferevent_flush_mode mode, void *ctx) {
    
    cout << "filter out" << endl;
    char data[1024] = {0};
    int len = evbuffer_remove(src, data, sizeof(data) - 1);
    string str = "";
    str += "===============\n";
    str += data;
    str += "===============\n";
    evbuffer_add(dst, str.c_str(), str.size());
    return BEV_OK;
}

void event_cb(bufferevent *bufev, short event_type, void *args) {
    cout << "event_cb" << endl;
}
void read_cb(bufferevent *bufev, void *args) {
    cout << "read_cb" << endl;
    char data[1024] = {0};
    int len = bufferevent_read(bufev, data, sizeof(data) - 1);
    cout << data << flush;
    bufferevent_write(bufev, data, len);
}
void write_cb(bufferevent *bufev, void *args) {
    cout << "write_cb" << endl;
}

void listener_cb(evconnlistener *ev, evutil_socket_t sock, sockaddr* addr, int addrlen, void *args) {
    event_base *base = (event_base*)args;
    cout << "listener_cb" << endl;
    // 创建 bufferevent
    bufferevent *bufev = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
    // 绑定过滤器
    bufferevent *bufev_filter = bufferevent_filter_new(bufev,   // 底层已经对进行过滤的 bufferevent 进行了读写事件的开启
        filter_in,  // 输入过滤
        filter_out, // 输出过滤
        BEV_OPT_CLOSE_ON_FREE,  // 关闭 filter 时也关闭 bufferevent
        0,  // 清理回调函数
        0); // 传递给回调的参数

    // 设置 bufferevent 回调
    bufferevent_setcb(bufev_filter, read_cb, write_cb, event_cb, NULL);

    bufferevent_enable(bufev_filter, EV_READ | EV_WRITE);   
    
    
}

int main() {
    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
        cerr << "set pipe signal error" << endl;
        return 1;
    }

    event_base *base = event_base_new();
    if (base) {
        cout << "create new event base" << endl;
    }

    sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    // inet_pton(AF_INET, ip, &sin.sin_addr);

    evconnlistener *ev = evconnlistener_new_bind(base, listener_cb, base, 
        LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, 5,
        (sockaddr*)&sin, sizeof(sin));
    // 进入事件主循环
    assert(base);
    event_base_dispatch(base);
    event_base_free(base);

}